package com.aaa.system.service.imp;

import com.aaa.entity.OperLog;
import com.aaa.system.mapper.OperLogMapper;
import com.aaa.system.service.OperLogService;
import com.aaa.system.vo.OperLogVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class OperLogServiceImp implements OperLogService {

    @Autowired
    private OperLogMapper operLogMapper;

    @Override
    public void OperLoginsert(OperLog operLog) {
        operLogMapper.insert(operLog);

    }

    @Override
    public Result<IPage<OperLog>> getAllOperLog(OperLogVo operLogVo) {
        IPage<OperLog> page = new Page<>(operLogVo.getCurrent(),operLogVo.getSize());
        QueryWrapper<OperLog> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(operLogVo.getTitle())){
            wrapper.like("title",operLogVo.getTitle());
        }
        if (StringUtils.isNotBlank(operLogVo.getOperName())){
            wrapper.like("oper_name",operLogVo.getOperName());
        }
        if (Objects.nonNull(operLogVo.getBusinessType())){
            wrapper.eq("business_type",operLogVo.getBusinessType());
        }
        if (Objects.nonNull(operLogVo.getStatus())){
            wrapper.eq("status",operLogVo.getStatus());
        }
        if (Objects.nonNull(operLogVo.getDateRange())&&operLogVo.getDateRange().length==2){
            wrapper.between("oper_time",operLogVo.getDateRange()[0],operLogVo.getDateRange()[1]);
        }
        IPage<OperLog> page1 = operLogMapper.selectPage(page, wrapper);
        return new Result<>(200,"查询成功",page1);
    }

    @Override
    public Result<Boolean> delOperLog(Long[] operIds) {
        int i = operLogMapper.bachOperLog(operIds);
        return new Result<>(200,"删除操作日志成功",i>0?true:false);
    }

    @Override
    public Result<String> clearOperLogTable() {
        operLogMapper.clearAllData();
        return new Result<>(200,"清除表单数据成功");
    }
}
